<template>
  <base-breadcrumb>
    <!-- 搜索 -->
    <searchData :searchData="searchArr" @search="search"></searchData>
    <!-- 表格 -->
    <a-card :bordered="false">
      <a-button type="primary" class="mb20" @click="handleManage({},'add')" v-if="hideBuquBtn()">添加检查记录</a-button>
      <list-table
        ref="table"
        :columns="columns"
        :request="loadData"
      >
        <template slot="action" slot-scope="text, record">
          <MoreButton>
            <a v-btn-permission="'hse_037_loo_04'" @click="handleManage(record, 'look')">查看</a>
            <a v-if="record.meetingEnum === 'UNSTART'&&hideBuquBtn()" v-btn-permission="'hse_037_edi_02'"
                  @click="handleManage(record, 'edit')"
            >编辑</a>
            <a v-if="record.meetingEnum === 'CRACKING'" v-btn-permission="'hse_037_dln_05'"
                  @click="download(record.id)"
            >下载</a>
            <a v-btn-permission="'hse_037_del_03'" @click="handleDelete(record)">删除</a>
            <a v-if="record.meetingEnum === 'TOSCORE'&&hideBuquBtn()" v-btn-permission="'hse_037_kpf_01'"
                  @click="handleManage(record,'score')"
            >考核评分</a>
            <a v-if="record.meetingEnum === 'HIDDENDANGER'&&hideBuquBtn()" v-btn-permission="'hse_037_yhx_06'"
                  @click="handleManage(record,'danger')"
            >添加隐患项</a>
          </MoreButton>
        </template>
      </list-table>
    </a-card>

  </base-breadcrumb>
</template>

<script>
import CollapseStatic from '@/views/quality/components/CollapseStatic' // 汇总
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目名称
import OrgTreeSelect from '@/components/OrgTreeSelect'
import rectifyNoticeApi from '@/api/safetySupervision/checkAndRectify/rectifyNotice'
import Selector from '@/components/Select/Selector'
import SelectTreeCommon from '@/components/Select/SelectTreeCommon'
import Api from '@/api/office/hazardIdentification'
import ASelectOrgPeople from '@/components/Select/ASelectOrgPeople'
import LApi from '@/api/safetySupervision/checkAndRectify/safeCheckRecord.js'
import BSelect from '@/components/BComponents/BSelect'
import request2 from '@/utils/request2'
import { hasLetter } from '@/utils/regExpression'

export default {
  name: 'a' + Date.now(),
  components: {
    CollapseStatic
  },
  data() {
    return {
      searchArr: [
        // { name: '所属分公司', type: SelectSubCompany, valueKey: 'branchComId',getAll: true, },
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // { name: '所属项目', type: SelectProject, valueKey: 'projectId', keyValue: 'branchComId',isSelf:false },
        {
          name: '检查级别', value: 'checkLevel', type: 'select', data: [
            { label: '公司级', value: '公司级' },
            { label: '分公司级', value: '分公司级' },
            { label: '项目级', value: '项目级' }
          ]
        },
        {
          name: '检查类型', valueKey: 'checkTypeId', type: BSelect,
          request: async () => {
            let res = await request2({
              url: '/cscec-quality-business/quality/types-label',
              params: { type: '0', code: '0' }
            })
            if (res.code === this.SUCCESS_CODE) {
              return { code: this.SUCCESS_CODE, data: res.data.filter(v => v.enabled) }
            }
            return { code: this.SUCCESS_CODE, data: [] }
          }
        },
        {
          name: '检查区域', valueKey: 'respAreaId', type: SelectTreeCommon,
          keyValue: 'searchQuery._checked',
          params: { sources: 0 },
          linkParamsKey: 'id',
          api: e => hasLetter.test(e.id) ? Promise.resolve([]) : Api.subPackArea({}, e)
        },
        { name: '组织人', type: ASelectOrgPeople, valueKey: 'organizeId' },
        {
          name: '检查时间',
          value: 'startTime',
          type: 'dateRange',
          attr: {
            rangeFormat: ['startTimeStart', 'startTimeEnd']
          }
        }
      ],
      columns: [
        { title: '检查单编号', dataIndex: 'number',width: 120,},
        { title: '检查级别', dataIndex: 'checkLevel',width: 100,},
        { title: '检查类型', dataIndex: 'checkTypeName',width: 120,},
        { title: '检查区域', dataIndex: 'respAreaName',width: 180,},
        { title: '分公司名称', dataIndex: 'branchComName',width: 120,},
        { title: '项目名称', dataIndex: 'projectName',width: 250,},
        { title: '组织人', dataIndex: 'organizeName',width: 120,},
        { title: '开始时间', dataIndex: 'startTime',width: 200,},
        { title: '隐患数', dataIndex: 'dangerSize',width: 100,},
        { title: '整改状态', dataIndex: 'rectificationStatus',width: 100,},
        { title: '得分', dataIndex: 'score',width: 100,},
        { title: '状态', dataIndex: 'meetingEnumStr',width: 120,},
        {
          title: '操作',
          dataIndex: 'action',
          width: 220,
          fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      btnVal: 0,
      queryParam: {}, // 查询参数

      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const q = Object.assign({}, parameter, { query: this.queryParam })
        if (q.query.startTimeStart) q.query.startTimeStart = q.query.startTimeStart + ' 00:00:00'
        if (q.query.startTimeEnd) q.query.startTimeEnd = q.query.startTimeEnd + ' 23:59:59'
        return LApi.findListByMain({}, q)
      }
    }
  },
  methods: {
    async download(id) {
      await LApi.download({ id })
      this.$refs.table.refresh()
    },
    /**
     * 搜索
     */
    search(value) {
      value && (this.queryParam = value)
      this.$refs.table.refresh()
    },
    /**
     * 操作栏
     */
    handleManage(record = {}, pageType) {
      this.$router.push({
        path: '/safetySupervision/checkAndRectify/safeCheckRecord/add',
        query: {
          id: record.id,
          pageType
        }
      })
    },
    /**
     * 删除记录
     */
    handleDelete(e) {
      this.$delList3(LApi.del, () => {
        this.search()
      }, { id: e.id })
    }
  }
}
</script>


